//
//  MainViewController.swift
//  WBDemo
//
//  Created by Jin on 16/04/2018.
//  Copyright © 2018 com.baidu.123. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    // MARK: - 视图生命周期
    override func viewDidLoad() {
        super.viewDidLoad()

        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.addSubview(composeButton)
        composeButton.center.x = tabBar.center.x
    }
    
    // MARK: - 事件响应
    @objc private func composeBtnClick() {
        let composeVc = ComposeViewController()
        let composeNav = UINavigationController(rootViewController: composeVc)
        present(composeNav, animated: true, completion: nil)
    }
    
    // MARK: - 懒加载
    private lazy var composeButton:UIButton = {
        let btn = UIButton(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
        //添加监听
        btn.addTarget(self, action: #selector(composeBtnClick), for: UIControlEvents.touchUpInside)
        btn.frame.size.width = UIScreen.main.bounds.size.width / CGFloat(self.childViewControllers.count)
        return btn
    }()
}


/*
 //load JSON data from Server
 let path = Bundle.main.path(forResource: "MainVcSettings.json", ofType: nil)!
 let data = NSData(contentsOfFile: path)!
 guard let objc = try? JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers) else {
 
 addChildViewController(childControllerName: "HomeTableViewController", imageName: "tabbar_home", title: "首页")
 addChildViewController(childControllerName: "MessageTableViewController", imageName: "tabbar_message_center", title: "消息")
 addChildViewController(childControllerName: "DiscoverTableViewController", imageName: "tabbar_discover", title: "广场")
 addChildViewController(childControllerName: "ProfileTableViewController", imageName: "tabbar_profile", title: "我的")
 
 return
 }
 
 for dict in objc as! [[String:AnyObject]] {
 addChildViewController(childControllerName: dict["vcName"] as! String, imageName: dict["imageName"] as! String, title: dict["title"] as! String)
 }
 
 */

/*
 func addChildViewController( childControllerName: String, imageName:String,title:String) {
 
 //动态获取命名空间
 guard let nsp = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
 return
 }
 
 //通过字符串创建控制器
 let cls:AnyClass? = NSClassFromString(nsp + "." + childControllerName)
 
 //在Swift中,如果想通过一个AnyClass来创建一个对象,必须先明确这个类的类型
 guard let vcCls = cls as? UITableViewController.Type else {
 return
 }
 let childController = vcCls.init()
 
 childController.tabBarItem.image = UIImage(named: imageName)
 childController.title = title
 childController.tabBarItem.selectedImage = UIImage(named: imageName+"_highlighted")
 let nav = UINavigationController.init(rootViewController: childController)
 addChildViewController(nav)
 }
 */

